﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Utility;
using Jzy.Core.Common.Basic;
using Jzy.BLL.Forum;
using Jzy.Model.Forum;
using Jzy.Core.Common.Web;

namespace Jzy.Web.Forum.Group
{
    public partial class List : PageList
    {       
        private ForumGroupBO Group
        {
            get
            {
                return ForumGroupBO.Instance<ForumGroupBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                WebUtil.BindListControlWithEnum(TypeID, typeof(ForumGroupType));
                TypeID.Items.Insert(0, new ListItem("全部", "-1"));
                TypeID.SelectedValue = "-1";
                BindView();
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
           
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            lv.DataKeyNames = new string[] { "ID" };
            IEnumerable<ForumGroupInfo> list = this.Group.GetList().OrderByDescending(T => T.TypeID).ThenBy(T => T.StarCount);

            lv.DataSource = list;
            lv.DataBind();

            lblStatistic.Text = "[&nbsp;共" + list.Count() + "个组&nbsp;]";
           
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "用户组");
            JSUtil.ClickBtn(lnkDelete, btnDelete);

            JSUtil.ShowForm(lnkAdd, "增加组", null, GlobalVar.WebCurrPath + "Edit.aspx", 450, 180);
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                
                LVUtil.SetEdit(sender, e, 450, 180);

                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());

                JSUtil.ShowForm(lnkEdit, "修改用户组", null, GlobalVar.WebCurrPath + "Edit.aspx?ID=" + strID, 450, 180);
                //LVUtil.SetEdit(sender, e, 450, 180, lnkEdit);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            this.Group.Delete(strSelectIDs, true);
            BindView();
        }
    }
}
